Skip to content

Conversation

bschoenmaeckers
Copy link

@bschoenmaeckers bschoenmaeckers commented Jul 31, 2025

I found myself using prop_assume!(result.is_ok()) a lot before unwrapping some Result value that I use while testing. So to make life easier I come to the following api addition.

This allows to write test like the following.

use jiff::civil::Date;
use proptest::prelude::*;

proptest! {
  #[test]
  fn test_some_date(
    year in 1i16..=9999i16,
    month in 1i8..=12i8,
    day in 1i8..=31i8
  ) {
    // Reject invalid dates
    let date = Date::new(year, month, day).prop_assume_ok()?;
    // ...rest of test...
  }
}


/// Extension trait for `Result<T, E>` to provide additional functionality
/// specifically for prop test cases.
pub trait ResultExt<T, E> {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is a nice ergonomic addition, but ResultExt is pretty common. can we name this ProptestResultExt instead?

also seems like a good case for a sealed trait
e.g. from blog post

mod private {
    pub trait Sealed {}
}

pub trait SealedTrait : private::Sealed {
    fn method(&self);
}

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added.


impl<T, E> ResultExt<T, E> for Result<T, E> {
#[track_caller]
fn assume_ok(self) -> Result<T, TestCaseError>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what do you think about naming prop_assume_ok?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds good to me. Changed.

@bschoenmaeckers bschoenmaeckers changed the title Add ResultExt with assume_ok helper Add ProptestResultExt with assume_ok helper Oct 19, 2025
@bschoenmaeckers bschoenmaeckers changed the title Add ProptestResultExt with assume_ok helper Add ProptestResultExt with prop_assume_ok helper Oct 19, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants